package UILayer;

import ModelLayer.CurrentAccount;
import CtrLayer.LeaseCtr;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class MachineManager extends JInternalFrame {
	
	private JTextField name_tf;
	private JTextField price_tf;
	private String id;
	private LeaseCtr machine_ctr;
	private JLabel inc_name_lbl;
	private JLabel inc_price_lbl;
	private JLabel[] empty_error_lbls;
	private boolean is;

	/**
	 * Create the frame.
	 */
	public MachineManager (String id, boolean is_incomplete) {
		super("Machine", true, true, true, true);
		machine_ctr=new LeaseCtr();
		is=is_incomplete;
		this.id=id;
		setBounds(100, 100, 293, 174);
		getContentPane().setLayout(null);
		
		
		JButton btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) { try_create_edit();
			}
		});
		btnOk.setBounds(10, 103, 89, 23);
		getContentPane().add(btnOk);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) { dispose();
			}
		});
		btnCancel.setBounds(178, 103, 89, 23);
		getContentPane().add(btnCancel);
		
		make_lbls();
		make_tfs();
		make_empty_error_lbls();
		make_logical_error_lbls();

	}

	private void make_logical_error_lbls()
	{

		inc_name_lbl = new JLabel("<html>The name is <br/>already taken.<html>");
		inc_name_lbl.setForeground(Color.RED);
		inc_name_lbl.setBounds(174, 20, 290, 32);
		inc_name_lbl.setVisible(false);
		getContentPane().add(inc_name_lbl);
		
		inc_price_lbl = new JLabel("<html>You must enter<br/> a valid price.</html>");
		inc_price_lbl.setForeground(Color.RED);
		inc_price_lbl.setBounds(174, 51, 290, 32);
		inc_price_lbl.setVisible(false);
		getContentPane().add(inc_price_lbl);

	}

	private void make_lbls()
	{
		JLabel lblGroupName = new JLabel("Name");
		lblGroupName.setBounds(10, 29, 78, 14);
		getContentPane().add(lblGroupName);
		
		JLabel lblPricePerDay = new JLabel("Price per day");
		lblPricePerDay.setBounds(10, 60, 78, 14);
		getContentPane().add(lblPricePerDay);
	}

	private void make_tfs()
	{

		
		name_tf = new JTextField();
		name_tf.setBounds(78, 26, 86, 20);
		getContentPane().add(name_tf);
		name_tf.setColumns(10);
		
		price_tf = new JTextField();
		price_tf.setBounds(78, 57, 86, 20);
		getContentPane().add(price_tf);
		price_tf.setColumns(10);
		if (CurrentAccount.give_rank().equals("employee"))
		{
			price_tf.setEditable(false);
		}
		

		if (id != null)
		{
			fill_tfs();
		}
		if (is)
		{
			name_tf.setEditable(false);
		}
	}

	private void fill_tfs()
	{
		String[] data = machine_ctr.display_machine(id);
		name_tf.setText(data[0]);
		price_tf.setText(data[1]);
	}

	private void try_create_edit()
	{
		if (check())
		{

			String new_name = name_tf.getText();
			double price;
			try
			{
				price = Double.parseDouble(price_tf.getText());
			}
			catch (Exception ex)
			{
				price = 0.0;
			}
			if (id==null)
			{
				if (!machine_ctr.is_such_machine_id(name_tf.getText()))
				{
					machine_ctr. add_machine(name_tf.getText(), price);
					JOptionPane.showMessageDialog(this,
							"Successully added a machine.", "Success",
							JOptionPane.INFORMATION_MESSAGE);
					this.dispose();
				}
				else
				{
					JOptionPane
							.showMessageDialog(
									this,
									"Couldn't add a machine.\nMachine with this name already exists.",
									"Error", JOptionPane.INFORMATION_MESSAGE);
				}
			}
			else
			{
				if (machine_ctr.is_such_machine_id(id))
				{
					machine_ctr.edit_machine(id, new_name, price);
					JOptionPane.showMessageDialog(this,
							"Successully edited a Machine's information.",
							"Success", JOptionPane.INFORMATION_MESSAGE);
					this.dispose();
				}
				else
				{
					JOptionPane.showMessageDialog(this,
							"Couldn't edit a machine.", "Error",
							JOptionPane.INFORMATION_MESSAGE);
				}
			}
		}
	}

	private boolean check()
	{
		inc_name_lbl.setVisible(false);
		inc_price_lbl.setVisible(false);
		empty_error_lbls[0].setVisible(false);
		empty_error_lbls[1].setVisible(false);
		boolean[] display_lbl = { false, false};
		if ((machine_ctr.is_such_machine_name(name_tf.getText())==true)&&(id==null))
		{
			display_lbl[0] = true;
		}

		if (!price_tf.getText().equals(""))
		{
			try
			{
				double newprice = Double.parseDouble(price_tf.getText());
				display_lbl[1] = false;
			}
			catch (Exception e)
			{
				display_lbl[1] = true;
			}
		}
		return display_error_lbls(display_lbl);
	}

	private boolean display_error_lbls(boolean[] display_lbl)
	{
		boolean returner = true;
		if (name_tf.getText().equals(""))
		{
			empty_error_lbls[0].setVisible(true);
			returner = false;
		}
		
		else if (display_lbl[0])
		{
			inc_name_lbl.setVisible(true);
			returner = false;
		}
		if (display_lbl[1])
		{
			inc_price_lbl.setVisible(true);
			returner = false;
		}
		if ((price_tf.getText().equals(""))&&(!CurrentAccount.give_rank().equals("employee")))
		{
			empty_error_lbls[1].setVisible(true);
			returner = false;
		}
		
		return returner;
	}

	private void make_empty_error_lbls()
	{
		int[][] coords = { { 174, 30 }, { 174, 61 } };
		empty_error_lbls = new JLabel[2];
		for (int i = 0; i <= 1; i++)
		{
			empty_error_lbls[i] = new JLabel("Must be filled.");
			empty_error_lbls[i].setForeground(Color.RED);
			empty_error_lbls[i].setBounds(coords[i][0], coords[i][1], 150, 14);
			empty_error_lbls[i].setVisible(false);
			getContentPane().add(empty_error_lbls[i]);
		}

	}

}
